﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Licenta1.Statements;

namespace Licenta1.Expressions
{
    class CollectionIndexExpressions : Expression
    {
        string listName;
        Expression index;

        public CollectionIndexExpressions(Context con, Expression id, Expression index)
            : base(con)
        {
            if (id != null && index != null)
            {
                listName = ((IdentifierExpression)id).Name;
                this.index = index;
            }
            else
            {
                listName = "";
                index = null;
            }
        }


        public override object Value
        {
            get
            {
                if (Convert.ToInt32(index.Value) >= 0 && listName != "")
                {
                    foreach (UserCollection uc in context.userCollections)
                        if (uc.NAME == listName)
                            return ((Expression)uc.COLLECTION[Convert.ToInt32(index.Value)]).Value;
                }
                return null;
            }
        }

        public string LNAME
        {
            get { return listName; }
        }

        public Expression INDEX
        {
            get { return index; }
        }
    }

    class CollectionFirstExpressions : Expression
    {
        string listName;

        public CollectionFirstExpressions(Context con, Expression id)
            : base(con)
        {
            if (id != null)
                listName = ((IdentifierExpression)id).Name;
            else listName = ""; 
        }


        public override object Value
        {
            get
            {
                if (listName != "")
                foreach (UserCollection uc in context.userCollections)
                    if (uc.NAME == listName)
                        return ((Expression)uc.First()).Value;
                return null;
            }
        }

        public string LNAME
        {
            get { return listName; }
        }

    }
    class CollectionLastExpressions : Expression
    {
        string listName;

        public CollectionLastExpressions(Context con, Expression id)
            : base(con)
        {
            if (id != null)
                listName = ((IdentifierExpression)id).Name;
            else listName = ""; 
        }


        public override object Value
        {
            get
            {
                if (listName != null)
                foreach (UserCollection uc in context.userCollections)
                    if (uc.NAME == listName)
                        return ((Expression)uc.Last()).Value;
                return null;
            }
        }

        public string LNAME
        {
            get { return listName; }
        }

    }

    class CollectionSizeExpressions : Expression
    {
        string listName;

        public CollectionSizeExpressions(Context con, Expression id)
            : base(con)
        {
            if (id != null)
                listName = ((IdentifierExpression)id).Name;
            else listName = ""; 
        }


        public override object Value
        {
            get
            {
                if (listName != "")
                foreach (UserCollection uc in context.userCollections)
                    if (uc.NAME == listName)
                        return uc.Size();
                return 0;
            }
        }

        public string LNAME
        {
            get { return listName; }
        }

    }
}